home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 2 / Amiga Tools 2.iso / tools / packer / tar / src / list.c < prev    next >
C/C++ Source or Header  |  1995-03-09  |  12KB  |  538 lines

  1. /*
  2.  * List a tar archive.
  3.  *
  4.  * Also includes support routines for reading a tar archive.
  5.  *
  6.  * Pubic Domain version written 26 Aug 1985 by John Gilmore (ihnp4!hoptoad!gnu)
  7.  
  8.  *
  9.  * @(#)list.c 1.31 11/5/87 Public Domain - gnu
  10.  */
  11. #include <stdio.h>
  12. #include <ctype.h>
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. #if !defined(MSDOS) && !defined(AMIGA)
  16. #include <sys/file.h>
  17. #endif    /* MSDOS */
  18.  
  19. #ifdef USG
  20. #include <sys/sysmacros.h>    /* major() and minor() defined here */
  21. #endif
  22.  
  23. char *ctime();                /* From libc.a */
  24.  
  25. #define    isodigit(c)    ( ((c) >= '0') && ((c) <= '7') )
  26.  
  27. #include "tar.h"
  28. #include "port.h"
  29.  
  30. #ifdef AMIGA
  31. int made_on_amiga;        /* from extract.c */
  32. #endif
  33.  
  34. long from_oct();            /* Decode octal number */
  35. void demode();                /* Print file mode */
  36.  
  37. union record *head;            /* Points to current archive header */
  38. struct stat hstat;            /* Stat struct corresponding */
  39. int head_standard;            /* Tape header is in ANSI format */
  40.  
  41. void print_header();
  42. void skip_file();
  43.  
  44.  
  45. /*
  46.  * Main loop for reading an archive.
  47.  */
  48. void
  49. read_and(do_something)
  50.     void (*do_something)();
  51. {
  52.     int status = 3;            /* Initial status at start of archive */
  53.     int prev_status;
  54.  
  55.     name_gather();            /* Gather all the names */
  56.     open_archive(1);        /* Open for reading */
  57.  
  58.     for(;;) {
  59.         prev_status = status;
  60.         status = read_header();
  61.         switch (status) {
  62.  
  63.         case 1:            /* Valid header */
  64.             /* We should decode next field (mode) first... */
  65.             /* Ensure incoming names are null terminated. */
  66.             head->header.name[NAMSIZ-1] = '\0';
  67.             
  68.             if (!name_match(head->header.name)) {
  69.                 /* Skip past it in the archive */
  70.                 userec(head);
  71.                 /* Skip to the next header on the archive */
  72.                 skip_file((long)hstat.st_size);
  73.                 continue;
  74.             }
  75.  
  76.             (*do_something)();
  77.             continue;
  78.  
  79.             /*
  80.              * If the previous header was good, tell them
  81.              * that we are skipping bad ones.
  82.              */
  83.         case 0:            /* Invalid header */
  84.             userec(head);
  85.             switch (prev_status) {
  86.             case 3:        /* Error on first record */
  87.                 annorec(stderr, tar);
  88.                 fprintf(stderr, "Hmm, this doesn't look like a tar archive.\n");
  89.                 /* FALL THRU */
  90.             case 2:        /* Error after record of zeroes */
  91.             case 1:        /* Error after header rec */
  92.                 annorec(stderr, tar);
  93.                 fprintf(stderr,
  94.                     "Skipping to next file header...\n");
  95.             case 0:        /* Error after error */
  96.                 break;
  97.             }
  98.             continue;
  99.  
  100.         case 2:            /* Record of zeroes */
  101.             userec(head);
  102.             status = prev_status;    /* If error after 0's */
  103.             if (f_ignorez)    
  104.                 continue;
  105.             /* FALL THRU */
  106.         case EOF:        /* End of archive */
  107.             break;
  108.         }
  109.         break;
  110.     };
  111.  
  112.     close_archive();
  113.     names_notfound();        /* Print names not found */
  114. }        
  115.  
  116.  
  117. /*
  118.  * Print a header record, based on tar options.
  119.  */
  120. void
  121. list_archive()
  122. {
  123.  
  124.     /* Save the record */
  125.     saverec(&head);
  126.  
  127.     /* Print the header record */
  128.     if (f_verbose) {
  129.         if (f_verbose > 1)
  130.             decode_header(head, &hstat, &head_standard, 0);
  131.         print_header(stdout);
  132.     }
  133.  
  134.     /* Skip past it in the archive */
  135.     saverec((union record **) 0);    /* Unsave it */
  136.     userec(head);
  137.  
  138.     /* Skip to the next header on the archive */
  139.     skip_file((long)hstat.st_size);
  140. }
  141.  
  142.  
  143. /*
  144.  * Read a record that's supposed to be a header record.
  145.  * Return its address in "head", and if it is good, the file's
  146.  * size in hstat.st_size.
  147.  *
  148.  * Return 1 for success, 0 if the checksum is bad, EOF on eof,
  149.  * 2 for a record full of zeros (EOF marker).
  150.  *
  151.  * You must always userec(head) to skip past the header which this
  152.  * routine reads.
  153.  */
  154. int
  155. read_header()
  156. {
  157.     register int    i;
  158.     register long    sum, recsum;
  159.     register char    *p;
  160.     register union record *header;
  161.  
  162.     header = findrec();
  163.     head = header;        /* This is our current header */
  164.     if (NULL == header) return EOF;
  165.  
  166.     recsum = from_oct(8,  header->header.chksum);
  167.  
  168.     sum = 0;
  169.     p = header->charptr;
  170.     for (i = sizeof(*header); --i >= 0;) {
  171.         /*
  172.          * We can't use unsigned char here because of old compilers,
  173.          * e.g. V7.
  174.          */
  175.         sum += 0xFF & *p++;
  176.     }
  177.  
  178.     /* Adjust checksum to count the "chksum" field as blanks. */
  179.     for (i = sizeof(header->header.chksum); --i >= 0;)
  180.         sum -= 0xFF & header->header.chksum[i];
  181.     sum += ' '* sizeof header->header.chksum;    
  182.  
  183.     if (sum == recsum) {
  184.         /*
  185.          * Good record.  Decode file size and return.
  186.          */
  187.         if (header->header.linkflag == LF_LINK)
  188.             hstat.st_size = 0;    /* Links 0 size on tape */
  189.         else
  190.             hstat.st_size = from_oct(1+12, header->header.size);
  191.         return 1;
  192.     }
  193.  
  194.     if (sum == 8*' ') {
  195.         /*
  196.          * This is a zeroed record...whole record is 0's except
  197.          * for the 8 blanks we faked for the checksum field.
  198.          */
  199.         return 2;
  200.     }
  201.  
  202.     return 0;
  203. }
  204.  
  205.  
  206. /* 
  207.  * Decode things from a file header record into a "struct stat".
  208.  * Also set "*stdp" to !=0 or ==0 depending whether header record is "Unix
  209.  * Standard" tar format or regular old tar format.
  210.  *
  211.  * read_header() has already decoded the checksum and length, so we don't.
  212.  *
  213.  * If wantug != 0, we want the uid/group info decoded from Unix Standard
  214.  * tapes (for extraction).  If == 0, we are just printing anyway, so save time.
  215.  *
  216.  * decode_header should NOT be called twice for the same record, since the
  217.  * two calls might use different "wantug" values and thus might end up with
  218.  * different uid/gid for the two calls.  If anybody wants the uid/gid they
  219.  * should decode it first, and other callers should decode it without uid/gid
  220.  * before calling a routine, e.g. print_header, that assumes decoded data.
  221.  */
  222. decode_header(header, st, stdp, wantug)
  223.     register union record    *header;
  224.     register struct stat    *st;
  225.     int    *stdp;
  226.     int    wantug;
  227. {
  228.     made_on_amiga = FALSE;
  229.     st->st_mode = from_oct(8,  header->header.mode);
  230.     st->st_mtime = from_oct(1+12, header->header.mtime);
  231. #ifdef AMIGA
  232.     /*
  233.      * Tar file made on Amiga, use the prot and comment in header
  234.      */
  235.     if (!strcmp("AmigaTar", header->header.magic_cookie))
  236.     {
  237.         made_on_amiga = TRUE;
  238.         st->st_prot = from_hex(header->header.amiga_modes);
  239.         strcpy(st->st_comment, header->header.comment);
  240.         st->st_date.ds_Days = from_hex(header->header.ds_Days);
  241.         st->st_date.ds_Minute = from_hex(header->header.ds_Minute);
  242.         st->st_date.ds_Tick = from_hex(header->header.ds_Tick);
  243.     }
  244. #endif
  245.     
  246.     if (0==strcmp(header->header.magic, TMAGIC)) {
  247.         /* Unix Standard tar archive */
  248.         *stdp = 1;
  249.         if (wantug) {
  250. #ifdef NONAMES
  251.             st->st_uid = from_oct(8,  header->header.uid);
  252.             st->st_gid = from_oct(8,  header->header.gid);
  253. #else
  254.             st->st_uid = finduid(header->header.uname);
  255.             st->st_gid = findgid(header->header.gname);
  256. #endif
  257.         }
  258.         switch  (header->header.linkflag) 
  259.         case LF_BLK: case LF_CHR:
  260.             st->st_rdev = makedev(from_oct(8, header->header.devmajor),
  261.                        from_oct(8, header->header.devminor));
  262.     } else {
  263.         /* Old fashioned tar archive */
  264.         *stdp = 0;
  265.         st->st_uid = from_oct(8,  header->header.uid);
  266.         st->st_gid = from_oct(8,  header->header.gid);
  267.         st->st_rdev = 0;
  268.     }
  269. }
  270.  
  271.  
  272. /*
  273.  * Quick and dirty octal conversion.
  274.  *
  275.  * Result is -1 if the field is invalid (all blank, or nonoctal).
  276.  */
  277. long
  278. from_oct(digs, where)
  279.     register int    digs;
  280.     register char    *where;
  281. {
  282.     register long    value;
  283.  
  284.     while (isspace(*where)) {        /* Skip spaces */
  285.         where++;
  286.         if (--digs <= 0)
  287.             return -1;        /* All blank field */
  288.     }
  289.     value = 0;
  290.     while (digs > 0 && isodigit(*where)) {    /* Scan til nonoctal */
  291.         value = (value << 3) | (*where++ - '0');
  292.         --digs;
  293.     }
  294.  
  295.     if (digs > 0 && *where && !isspace(*where))
  296.         return -1;            /* Ended on non-space/nul */
  297.  
  298.     return value;
  299. }
  300.  
  301.  
  302. /*
  303.  * Actually print it.
  304.  *
  305.  * Plain and fancy file header block logging.
  306.  * Non-verbose just prints the name, e.g. for "tar t" or "tar x".
  307.  * This should just contain file names, so it can be fed back into tar
  308.  * with xargs or the "-T" option.  The verbose option can give a bunch
  309.  * of info, one line per file.  I doubt anybody tries to parse its
  310.  * format, or if they do, they shouldn't.  Unix tar is pretty random here
  311.  * anyway.
  312.  *
  313.  * Note that print_header uses the globals <head>, <hstat>, and
  314.  * <head_standard>, which must be set up in advance.  This is not very clean
  315.  * and should be cleaned up.  FIXME.
  316.  */
  317. #define    UGSWIDTH    11        /* min width of User, group, size */
  318. #define    DATEWIDTH    19        /* Last mod date */
  319. static int    ugswidth = UGSWIDTH;    /* Max width encountered so far */
  320.  
  321. void
  322. print_header(outfile)
  323.     FILE *outfile;
  324. {
  325.     char modes[11];
  326.     char *timestamp;
  327.     char uform[11], gform[11];    /* These hold formatted ints */
  328.     char *user, *group;
  329.     char size[24];        /* Holds a formatted long or maj, min */
  330.     long longie;        /* To make ctime() call portable */
  331.     int    pad;
  332.  
  333.     annofile(outfile, (char *)NULL);
  334.  
  335.     if (f_verbose <= 1) {
  336.         /* Just the fax, mam. */
  337.         fprintf(outfile, "%s\n", head->header.name);
  338.         return;
  339.     } else {
  340.         /* File type and modes */
  341.         modes[0] = '?';
  342.         switch (head->header.linkflag) {
  343.         case LF_NORMAL:
  344.         case LF_OLDNORMAL:
  345.         case LF_LINK:
  346.                 modes[0] = '-'; 
  347.                 if ('/' == head->header.name[strlen(head->header.name)-1])
  348.                     modes[0] = 'd';
  349.                 break;
  350.         case LF_DIR:    modes[0] = 'd'; break;
  351.         case LF_SYMLINK:modes[0] = 'l'; break;
  352.         case LF_BLK:    modes[0] = 'b'; break;
  353.         case LF_CHR:    modes[0] = 'c'; break;
  354.         case LF_FIFO:    modes[0] = 'p'; break;    
  355.         case LF_CONTIG:    modes[0] = 'C'; break;
  356.         }
  357.  
  358.         demode((unsigned)hstat.st_mode, modes+1);
  359.  
  360.         /* Timestamp */
  361.         longie = hstat.st_mtime;
  362.         timestamp = ctime(&longie);
  363.         timestamp[16] = '\0';
  364.         timestamp[24] = '\0';
  365.  
  366.         /* User and group names */
  367.         if (*head->header.uname && head_standard) {
  368.             user  = head->header.uname;
  369.         } else {
  370.             user = uform;
  371.             (void)sprintf(uform, "%d", (int)hstat.st_uid);
  372.         }
  373.         if (*head->header.gname && head_standard) {
  374.             group = head->header.gname;
  375.         } else {
  376.             group = gform;
  377.             (void)sprintf(gform, "%d", (int)hstat.st_gid);
  378.         }
  379.  
  380.         /* Format the file size or major/minor device numbers */
  381.         switch (head->header.linkflag) {
  382.         case LF_CHR:
  383.         case LF_BLK:
  384.             (void)sprintf(size, "%d,%d",
  385.                     major(hstat.st_rdev),
  386.                     minor(hstat.st_rdev));
  387.             break;
  388.  
  389.         default:
  390.             (void)sprintf(size, "%ld", (long)hstat.st_size);
  391.         }
  392.  
  393.         /* Figure out padding and print the whole line. */
  394.         pad = strlen(user) + strlen(group) + strlen(size) + 1;
  395.         if (pad > ugswidth) ugswidth = pad;
  396.  
  397.         fprintf(outfile, "%s %s/%s %*s%s %s %s %.*s",
  398.             modes,
  399.             user,
  400.             group,
  401.             ugswidth - pad,
  402.             "",
  403.             size,
  404.             timestamp+4, timestamp+20,
  405.             sizeof(head->header.name),
  406.             head->header.name);
  407.  
  408.         switch (head->header.linkflag) {
  409.         case LF_SYMLINK:
  410.             fprintf(outfile, " -> %s\n", head->header.linkname);
  411.             break;
  412.  
  413.         case LF_LINK:
  414.             fprintf(outfile, " link to %s\n", head->header.linkname);
  415.             break;
  416.  
  417.         default:
  418.             fprintf(outfile, " unknown file type '%c'\n",
  419.                 head->header.linkflag);
  420.             break;
  421.  
  422.         case LF_OLDNORMAL:
  423.         case LF_NORMAL:
  424.         case LF_CHR:
  425.         case LF_BLK:
  426.         case LF_DIR:
  427.         case LF_FIFO:
  428.         case LF_CONTIG:
  429.             putc('\n', outfile);
  430.             break;
  431.         }
  432.     }
  433. }
  434.  
  435. /*
  436.  * Print a similar line when we make a directory automatically.
  437.  */
  438. void
  439. pr_mkdir(pathname, length, mode, outfile)
  440.     char *pathname;
  441.     int length;
  442.     int mode;
  443.     FILE *outfile;
  444. {
  445.     char modes[11];
  446.  
  447.     if (f_verbose > 1) {
  448.         /* File type and modes */
  449.         modes[0] = 'd';
  450.         demode((unsigned)mode, modes+1);
  451.  
  452.         annofile(outfile, (char *)NULL);
  453.         fprintf(outfile, "%s %*s %.*s\n",
  454.             modes,
  455.             ugswidth+DATEWIDTH,
  456.             "Creating directory:",
  457.             length,
  458.             pathname);
  459.     }
  460. }
  461.  
  462.  
  463. /*
  464.  * Skip over <size> bytes of data in records in the archive.
  465.  */
  466. void
  467. skip_file(size)
  468.     register long size;
  469. {
  470.     union record *x;
  471.  
  472.     while (size > 0) {
  473.         x = findrec();
  474.         if (x == NULL) {    /* Check it... */
  475.             annorec(stderr, tar);
  476.             fprintf(stderr, "Unexpected EOF on archive file\n");
  477.             exit(EX_BADARCH);
  478.         }
  479.         userec(x);
  480.         size -= RECORDSIZE;
  481.     }
  482. }
  483.  
  484.  
  485. /*
  486.  * Decode the mode string from a stat entry into a 9-char string and a null.
  487.  */
  488. void
  489. demode(mode, string)
  490.     register unsigned mode;
  491.     register char *string;
  492. {
  493.     register unsigned mask;
  494.     register char *rwx = "rwxrwxrwx";
  495.  
  496.     for (mask = 0400; mask != 0; mask >>= 1) {
  497.         if (mode & mask)
  498.             *string++ = *rwx++;
  499.         else {
  500.             *string++ = '-';
  501.             rwx++;
  502.         }
  503.     }
  504.  
  505.     if (mode & S_ISUID)
  506.         if (string[-7] == 'x')
  507.             string[-7] = 's';
  508.         else
  509.             string[-7] = 'S';
  510.     if (mode & S_ISGID)
  511.         if (string[-4] == 'x')
  512.             string[-4] = 's';
  513.         else
  514.             string[-4] = 'S';
  515.     if (mode & S_ISVTX)
  516.         if (string[-1] == 'x')
  517.             string[-1] = 't';
  518.         else
  519.             string[-1] = 'T';
  520.     *string = '\0';
  521. }
  522.  
  523. #ifdef AMIGA
  524. from_hex(char *s)
  525. {
  526.     int i, val = 0;
  527.     char nibble;
  528.  
  529.     for (i = 0; i < 8; i++)
  530.     {
  531.     nibble = *s++;
  532.     nibble = (nibble >= 'a') ? 10 + (nibble - 'a') : nibble - '0';
  533.     val = (val << 4) + nibble;
  534.     }
  535.     return(val);
  536. }
  537. #endif
  538.